package com.pms.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import com.pms.entity.MessageComment;
import com.pms.util.DBHelp;
import com.pms.util.RowMapper;

public class MessageCommentDao {

	DBHelp<MessageComment> db = new DBHelp<MessageComment>();
	
	public void save(MessageComment mesComment){
		String sql = "INSERT INTO t_mescomment(t_message_mesid, t_user_useid, mescomment, commenttime) VALUE(?,?,?,?)";
		db.executeSQL(sql, mesComment.getMesid(), mesComment.getUserid(), mesComment.getComment(), mesComment.getCommenttime());
	}

	public List<MessageComment> findMesComByMesId() {
		StringBuilder sb = new StringBuilder();
		sb.append("SELECT tmc.t_message_mesid, tmc.t_user_useid, tmc.mescomment, tmc.commenttime, tu.`username` ");
		sb.append("FROM t_mescomment AS tmc, t_message AS tm, t_user AS tu ");
		sb.append("WHERE tmc.t_message_mesid = tm.mesid AND tmc.`t_user_useid` = tu.`useid` ");
		return db.executeQueryToList(sb.toString(), new RowMapper<MessageComment>(){

			@Override
			public MessageComment mapRow(ResultSet rs) throws SQLException {
				MessageComment mesCom = new MessageComment();
				mesCom.setMesid(rs.getString("t_message_mesid"));
				mesCom.setUserid(rs.getString("t_user_useid"));
				mesCom.setComment(rs.getString("mescomment"));
				mesCom.setUsername(rs.getString("username"));
				mesCom.setCommenttime(rs.getString("commenttime"));
				return mesCom;
			}
		});
	} 
}
 